#include "StdAfx.h"
#include "planet.h"
#include "player.h"

const int defaultPenWidth = 2;
const unsigned int defaultWidthFactor = 4;
const unsigned int defaultHeightFactor = 4;
const unsigned int defaultPlanetSize = 64;
const QColor defaultPenColor = QColor(0xff,0xff,0xff);
const qreal countOfShipsInSecondAddend = 0.5;
Planet::Planet(PlanetID planetID, double xCoord, double yCoord, GameColors planetColor, GamePlanets planetNumber , 
               unsigned int countOfShips, PlanetSize planetSize, PlayerPtr player, 
               QGraphicsItem *parent)
               : QGraphicsObject(parent)
               , m_borderPen(GameColor[planetColor])
               , m_countOfShips(countOfShips)
               , m_pixmap(Planets[planetNumber])
               , m_planetID(planetID)
               , m_pressed(false)
               , m_planetOwner(player)
               , m_planetSize(planetSize)
               , m_xCoord(xCoord)
               , m_yCoord(yCoord)
{
   init();
}

Planet::~Planet()
{
   qDebug() << "Planet with id " << m_planetID << " is being destructed";
   m_planetOwner.reset();
   disconnectSignals();
}

void Planet::paint( QPainter *painter, const QStyleOptionGraphicsItem * option, QWidget * /* = 0 */ )
{
   if (m_planetOwner)
   {
      m_borderPen.setColor(m_planetOwner->PlayerColor());
      painter->setPen(m_planetOwner->PlayerColor());
   }
   else
   {
      m_borderPen.setColor(defaultPenColor);
      painter->setPen(defaultPenColor);
   }
   
   painter->drawPixmap(0, 0, m_pixmap);
   painter->setFont(*m_font);
   painter->drawText(boundingRect(), Qt::AlignCenter, QString::number(static_cast<unsigned int>(m_countOfShips)));

   if (option->state & QStyle::State_Selected)
   {
      painter->drawEllipse(boundingRect());
   }
}

QRectF Planet::boundingRect() const
{
   return QRectF(-1, -1, m_pixmap.width() + 1, m_pixmap.height() + 1);
}

void Planet::init()
{
   setFlags(ItemIsSelectable);
   setAcceptHoverEvents(true);
   m_borderPen.setWidth(defaultPenWidth);
   m_font = boost::make_shared<QFont>("Times", 14, QFont::Bold);
   m_pixmap = m_pixmap.scaled(defaultPlanetSize + defaultWidthFactor * m_planetSize, defaultPlanetSize + defaultHeightFactor * m_planetSize);
   translate(-m_pixmap.width()/2, -m_pixmap.height()/2);
}

void Planet::increaseCountOfShips()
{
   m_countOfShips += countOfShipsInSecondAddend * (m_planetSize + 1);
}

void Planet::hoverEnterEvent( QGraphicsSceneHoverEvent *event)
{
   qDebug() << "Planet::hoverEnterEvent()";
   QGraphicsItem::hoverEnterEvent(event);
   emit hover(this);
}

void Planet::hoverLeaveEvent( QGraphicsSceneHoverEvent *event)
{
   qDebug() << "Planet::hoverLeaveEvent";
   QGraphicsItem::hoverLeaveEvent(event);
   emit hover(NULL);
}

void Planet::mouseReleaseEvent( QGraphicsSceneMouseEvent *event)
{
   qDebug() << "Planet::mouseReleaseEvent( QGraphicsSceneMouseEvent *event)";
   QGraphicsItem::mouseReleaseEvent(event);

   if (m_pressed)
   {
      m_pressed = false;
      emit itemMouseRelease(shared_from_this());
   }
}

void Planet::mousePressEvent( QGraphicsSceneMouseEvent *event)
{
   qDebug() << "Planet::mousePressEvent( QGraphicsSceneMouseEvent *event)";
   QGraphicsItem::mousePressEvent(event);
   m_pressed = true;
}

void Planet::disconnectSignals()
{
   qDebug() << "Planet destructor";
   disconnect();
   dumpObjectInfo();
}

PlayerID Planet::GetPlanetOwnerID() const
{
   if (m_planetOwner)
      return m_planetOwner->GetPlayerID();
   else
      return undefinedPlayerID;
}

QPainterPath Planet::shape() const
{
   QPainterPath path;
   path.addEllipse(boundingRect());
   return path;
}